exports.worksType = [
  '全部', '油画', '水彩', '国画', '工艺品', '彩铅', '少儿', '其它'
]

exports.resetTitle = function (value) {
  document.title = value
}

exports.validateMobile = function (value) {
  var res = /^1\d{10}$/
  return (res.test(value))
}

exports.validatePassword = function (value) {
  var res = /^[0-9a-zA-Z]{6,13}$/
  return (res.test(value))
}

var getCookie = function(name) {
  var match = document.cookie.match(new RegExp(name + '=([^;]+)'))
  if (match) return match[1]
  else return null
}
exports.getCookie = getCookie

exports.setCookie = function(cname, cvalue, exdays) {
  exdays = exdays ? exdays : 30;
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires=" + d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";expires=" + expires + ";path=/";
}

exports.delCookie = function(name) { 
  var exp = new Date(); 
  exp.setTime(exp.getTime() - 1); 
  var cval = getCookie(name);
  if(cval!=null){
      document.cookie= name + "=" + cval+";expires=" + exp.toGMTString() + ";path=/"; 
  }
}

exports.checkUserInfo = function (callBack) {
  // 判断用户登录信息
  // 项目开始没考虑这个问题，现在只能放这里了
  let userInfo = JSON.parse(getCookie('userInfo')) || ''
  if (!userInfo) {
    window.location.href = window.location.href.split('#/')[0].split('?')[0] + '#/login'
  } else {
    if (!userInfo.id) {
      // 微信报错的情况下
      window.location.href = window.location.href.split('#/')[0].split('?')[0] + '#/login'
    } else {
      callBack(userInfo)
    }
  }
}

exports.thumbsFn = function(api, id, type, callback) {
  // 1 作品  2 文章  3 动态(时光)  4 艺术圈  5 作者
  let params = {
    objId: id,
    type: type
  }
  api.get('/thumbs/add', params)
    .then(res => {
      if (res.code === '200') {
        callback(res.data)
      }
    })
}

exports.thumbsCancelFn = function(api, id, type, callback) {
  // 取消点赞
  // 1 作品  2 文章  3 动态(时光)  4 艺术圈  5 作者
  let params = {
    objId: id,
    type: type
  }
  api.get('/thumbs/d', params)
    .then(res => {
      if (res.code === '200') {
        callback(res.data)
      }
    })
}

exports.getAppid = function (api, getTicket, options) {
  let params = {
    url: encodeURIComponent(window.location.href)
  }
  api.get('/auth/jssdk', params)
    .then(res => {
      if (res.code === '200') {
        // 通用分享，配置微信sdk
        let opts = {}
        if (options) {
          opts.title = options.title
          opts.link = options.link
          opts.imgUrl = options.imgUrl
        } else {
          opts = {
            title: '沙塔艺堡欢迎您', 
            link: window.location.href, 
            imgUrl: res.data.logoUrl, // 分享图标
          }
        }
        getTicket(res.data, () => {
          alert('分享成功')
        }, () => {
          alert('分享失败')
        }, opts)
      }
    })
}